/**
 * @Author：ming
 * @Date：2023/10/4 8:27 PM
 * @Filename：Lc648
 */

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author ming
 */
public class Lc648 {
    public String replaceWords(List<String> dictionary, String sentence) {
        Collections.sort(dictionary, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.length()-o2.length();
            }
        });
        String[] split = sentence.split(" ");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < split.length; i++) {
            for (String s : dictionary) {
                if(split[i].startsWith(s)){
                    split[i] = s;
                    break;
                }
            }
            sb.append(split[i]).append(" ");
        }
     return   sb.toString().trim();
    }
}
